<template>
    <div>
        <el-dialog title="新增" :close-on-click-modal="false" :visible.sync="visible">
            <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
                <el-form-item label="资源类型" prop="type">
                    <el-radio-group v-model="dataForm.type">
                        <el-radio v-for="(type, index) in dataForm.typeList" :label="index" :key="index">{{ type }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item :label="dataForm.typeList[dataForm.type] + '名称'" prop="name">
                    <el-input v-model="dataForm.name" :placeholder="dataForm.typeList[dataForm.type] + '名称'" clearable></el-input>
                </el-form-item>
                <el-form-item label="父级菜单" prop="parentName">
                    <el-popover ref="menuListPopover" placement="bottom-start" trigger="click">
                        <el-tree :data="menuListAll" :props="menuListTreeProps" node-key="id" ref="menuListTree" @current-change="treeCurrentChangeHandle" :default-expand-all="false" :highlight-current="true" :expand-on-click-node="false" empty-text="暂无数据">
                        </el-tree>
                    </el-popover>
                    <el-input v-model="dataForm.parentName" v-popover:menuListPopover :readonly="true" placeholder="点击选择上级菜单" class="menu-list__input"></el-input>
                </el-form-item>
                <el-form-item v-if="dataForm.type === 1" label="菜单路由" prop="url">
                    <el-input v-model="dataForm.url" placeholder="菜单路由"></el-input>
                </el-form-item>
                <el-form-item v-if="dataForm.type !== 0" label="授权标识" prop="perms">
                    <el-input v-model="dataForm.perms" placeholder="多个用逗号分隔, 如: user:list,user:create"></el-input>
                </el-form-item>
                <el-form-item v-if="dataForm.type !== 2" label="排序号" prop="orderNum">
                    <el-input-number v-model="dataForm.orderNum" controls-position="right" :min="0" label="排序号"></el-input-number>
                </el-form-item>
                <el-form-item v-if="dataForm.type !== 2" label="菜单图标" prop="icon">
                    <el-row>
                        <el-col :span="22">
                            <el-popover ref="iconListPopover" placement="bottom-start" trigger="click" popper-class="mod-menu__icon-popover">
                                <div class="mod-menu__icon-inner">
                                    <div class="mod-menu__icon-list">
                                        <el-button v-for="(item, index) in iconList" :key="index" @click="iconActiveHandle(item)" :class="{ 'is-active': item === dataForm.icon }">
                                            <icon-svg :name="item"></icon-svg>
                                        </el-button>
                                    </div>
                                </div>
                            </el-popover>
                            <el-input v-model="dataForm.icon" v-popover:iconListPopover :readonly="true" placeholder="菜单图标名称" class="icon-list__input"></el-input>
                        </el-col>
                        <el-col :span="2" class="icon-list__tips">
                            <el-tooltip placement="top" effect="light">
                                <div slot="content">全站推荐使用SVG Sprite, 详细请参考:<a href="//github.com/daxiongYang/renren-fast-vue/blob/master/src/icons/index.js" target="_blank">icons/index.js</a>描述</div>
                                <i class="el-icon-warning"></i>
                            </el-tooltip>
                        </el-col>
                    </el-row>
                </el-form-item>
                 <el-form-item label="是否显示" prop="visibleCode">
                    <el-switch v-model="dataForm.visibleCode" active-color="#13ce66" inactive-color="#ff4949">
                    </el-switch>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="visible = false">取消</el-button>
                <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import Icon from "@/icons";
export default {
    data() {
        return {
            visible: false,
            dataRule: [],
            dataForm: {
                type: "0",
                typeList: ["目录", "菜单", "按钮"],
                name: "",
                parentName: "",
                url: "",
                orderNum: "",
                icon: "",
                visibleCode: true,
                perms: ""
            },
            menuListAll: [],
            menuListTreeProps: {
                children: "children",
                label: "resourceName",
            },
            iconList: [],
        };
    },
    created() {},
    mounted() {
        this.iconList = Icon.getNameList();
        console.log(this.dataForm.type)
    },
    methods: {
        init(list) {
            this.visible = true;
            this.menuListAll = list;
        },
        treeCurrentChangeHandle(data) {
            this.dataForm.parentName = data.resourceName;
        },
        iconActiveHandle(item) {
            this.dataForm.icon = item;
        },
        dataFormSubmit() {
            this.$api.callName
                .asidemenuAddList({
                    icon: this.dataForm.icon,
                    orderNum: this.dataForm.orderNum,
                    parentResourceId: "0000",
                    permission: this.dataForm.perms,
                    remark: this.dataForm.name,
                    resourceId: "2002",
                    resourceName: this.dataForm.name,
                    resourceTypeCode: "A",
                    url: this.dataForm.url,
                    visibleCode: this.dataForm.visibleCode.toString(),
                })
                .then((res) => {
                    if (res.data.code === 0) {
                        this.$message({
                            message: "操作成功",
                            type: "success",
                            duration: 1500,
                            onClose: () => {
                                this.visible = false;
                                this.$emit("refreshDataList");
                            },
                        });
                    } else {
                        console.log(5555);
                        this.$message.error(data.message);
                    }
                });
        },
    },
};
</script>
<style lang="scss">
$pColor: red;
p {
    color: $pColor;
}
</style>